package com.kk.permission.holder;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.airbnb.lottie.LottieAnimationView;
import com.kk.R;

public class PermissionStepViewHolder extends RecyclerView.ViewHolder {
    public TextView itemName;
    public LottieAnimationView itemStatusLottie;
    public ImageView itemStatus;

    public PermissionStepViewHolder(@NonNull View itemView) {
        super(itemView);
        itemName = itemView.findViewById(R.id.item_name);
        itemStatus = itemView.findViewById(R.id.item_status);
        itemStatusLottie = itemView.findViewById(R.id.item_status_lottie);
    }

    public void setStatus(boolean done, boolean playAnim) {
        if (itemStatus != null) {
            if (!done) {
                itemName.setTextColor(itemName.getResources().getColor(R.color.acc_step_item_text_color));
                itemStatus.setImageResource(R.drawable.acc_ic_undone);
            } else {
                if (playAnim && itemStatusLottie != null) {
                    itemStatus.setVisibility(View.INVISIBLE);
                    itemStatusLottie.setVisibility(View.VISIBLE);
                    itemStatusLottie.addAnimatorListener(new AnimatorListenerAdapter() {
                        @Override
                        public void onAnimationEnd(Animator animation) {
                            if (itemStatusLottie != null)
                                itemStatusLottie.setVisibility(View.GONE);
                            if (itemStatus != null) {
                                itemStatus.setImageResource(R.drawable.acc_ic_done);
                                itemStatus.setVisibility(View.VISIBLE);
                            }
                        }
                    });
                    itemStatusLottie.playAnimation();
                } else {
                    itemStatus.setImageResource(R.drawable.acc_ic_done);
                }
                itemName.setTextColor(itemName.getResources().getColor(R.color.acc_step_item_text_done_color));
            }
        }
    }
}
